<?php
if (!defined("MCTDESIGN")) die("# ");

    $thaotac = $_GET['act'];
    switch($thaotac){
        case "update":
           $html_body= updategallery();
            break;
        case "add":
           $html_body= save_galery();
            break;
        case "edit":
            $html_body = get_edit_gallery();
            break;
        case "xoaone":
            $html_body = delete_galery();
            break;
        case "images":
            $html_body = index_images();
            break;
        case "images_add":
            $html_body = images_add();
            break;
        case "xoa_images":
            $html_body = xoa_images();
            break;
        case "images_edit":
            $html_body = getEdit_images();
            break;
        case "upload_gallery":
        	$html_body = get_upload_nhieuhinh();    
        	break;
			
        default:
            $html_body= index_galery();
            break;
    }
/**
 * @author : pavennt 
 * Enter description load nhieu hinh
 */
function get_upload_nhieuhinh(){
    global $dir_tpl, $mysql, $tb_fix,$admin_id,$pid,$_c,$temp,$anyDB,$site_path;
	$id_gallery = $_POST['id'];
    $file_ary = reArrayFiles($_FILES['h_img']);
        foreach ($file_ary as $file) {
            $fileName =  $file['name'];
            $fileTmpName = $file['tmp_name'];
            if(!empty($fileName)){
                $post_img = @uploadIMG($fileName,$fileTmpName,"galery/images");
                $sql = "insert into mct_galery_images (gale_id,imag_url) values ('$id_gallery','$post_img')"; 
                $anyDB->query($sql);    
            }
        }
	$html_body =_html_trans(" Cập nhật bộ sưu tập thành công!",'?obj=gallery&act=edit&id='.$id_gallery.'#tabs-2');
 return $html_body;

}
function reArrayFiles(&$file_post) {
    // sap xep lai mang $_FILE khi su dung multi upload
    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}    
    
/**
* @author pavennt@gmail.com 
* Get all galery or form add galery
* 
*/
  function index_galery(){
    global $dir_tpl, $mysql, $tb_fix,$admin_id,$pid,$_c,$temp,$anyDB,$site_path;
    $temp_body = $temp;

    $sql ="select * from ".$tb_fix."_galery where gale_public= 1" ;
    $q = $anyDB->query($sql);
    $row = $anyDB->getAll($q);
    $temp->assign('galery',$row);
    $temp->assign('thumuc',$dir_tpl);
    
    $temp_body->assign("breakcum",breakcum("Quản lý Bộ sưu tập"));
	$_html = $temp_body->fetch( '_adm_galery.mct' );
     
    return $_html ;
  }
/**
 * @author pavennt@gmail.com
 * Description : them moi galery 
 * 
 */      
  function save_galery(){
    global $dir_tpl, $mysql, $tb_fix,$admin_id,$pid,$_c,$temp,$anyDB;
    $loai_gallery = $_POST['gallery_nuoc'];
    $gallery_name = $_POST['glery_name'];
    $gallery_description = $_POST['glery_decription'];
    $pro_img = isset($_FILES['gallery_img']['name'] ) ? $_FILES['gallery_img']['name'] : '';
    $pro_tmp_img = isset($_FILES['gallery_img']['tmp_name'] ) ? $_FILES['gallery_img']['tmp_name'] : '';
    $pro_img = uploadIMG($pro_img,$pro_tmp_img,"galery");
	if($pro_tmp_img == ''){
		$html_body =  _html_trans("Bạn chưa nhập đầy đủ dữ liệu  !","?obj=gallery");
	  return $html_body;
	}
	$sql ="insert into ".$tb_fix."_galery (gale_name,gale_img,gale_description,gallery_type) values ('$gallery_name','$pro_img','$gallery_description','$loai_gallery')"; 
    $q = $anyDB->query($sql);
    if($q){
        $html_body =  _html_trans("Đã thêm danh mục galery thành công !","?obj=gallery");
    }else{
        $html_body =  _html_trans("Có lỗi xảy ra  !","?obj=gallery");
    }
    return $html_body;
    
  } 
/**
 * @author : Nguyen tien pa ven 
 * Description : thao tac get galery to form edit
 * 
 */ 
 function get_edit_gallery(){
     global $dir_tpl, $mysql, $tb_fix,$admin_id,$pid,$_c,$temp,$anyDB,$site_path;
     $idgallery = $_GET['id'];
     $sql = "select * from ".$tb_fix."_galery where gale_id =".$idgallery;
     $getOne = $anyDB->query($sql);
     $arr = $anyDB->getNext($getOne); 
     $temp->assign("breakcum",breakcum("Sửa bộ sưu tập"));
     $temp->assign('gallery',$arr);
     $temp->assign('thumuc',$dir_tpl);
     $temp->assign('base_url',$site_path);
     
     // get hinh anh 
     $sql = "SELECT * FROM mct_galery_images WHERE gale_id = ".$idgallery;
     $resul = $anyDB->query($sql);
     $arr_images = $anyDB->getAll($resul);
     $temp->assign('arr_images' ,$arr_images);
     
     $_html = $temp->fetch( '_admin_gallery_edit.mct' );
     
    return $_html ;
     
  }
/**
 *@autho : pavennt
 *upadte galery category
 *
 */ 
function updategallery(){
	global $dir_tpl, $mysql, $tb_fix,$admin_id,$pid,$_c,$temp,$anyDB;
	$loai_gallery = $_POST['gallery_nuoc'];
    $name = $_POST['ten_galery'];
	$mota = $_POST['cat_description'];
	$id = $_POST['id'];
        $pro_img = isset($_FILES['cat_img']['name'] ) ? $_FILES['cat_img']['name'] : '';
	$pro_tmp_img = isset($_FILES['cat_img']['tmp_name'] ) ? $_FILES['cat_img']['tmp_name'] : '';
    if(!empty($pro_img) && !empty($pro_tmp_img)){
		$pro_img = uploadIMG($pro_img,$pro_tmp_img,"galery");
		$sql = "update ".$tb_fix."_galery set gale_name='$name' ,gale_img='$pro_img' ,gale_description ='$mota',gallery_type = '$loai_gallery' where gale_id= $id";
		
	}else{
		$sql = "update ".$tb_fix."_galery set gale_name='$name' ,gale_description ='$mota',gallery_type = '$loai_gallery' where gale_id= $id";
	}
    $q = $anyDB->query($sql);
    if($q){
        $html_body =  _html_trans("Sửa danh mục galery thành công !","?obj=gallery");
    }else{
        $html_body =  _html_trans("Có lỗi xảy ra  !","?obj=gallery");
    }
    return $html_body;	
	
} 
/**
 * @author pavennt
 * xoa galery ajax
 */
function delete_galery(){
    global $dir_tpl, $mysql, $tb_fix,$admin_id,$pid,$_c,$temp,$anyDB;
    $id = $_GET['id'];
    $sql = "select gale_img from ".$tb_fix."_galery where gale_id=$id";
    $q = $anyDB->query($sql);
    $arr = $anyDB->getNext($q);
    @unlink($arr['gale_img']);
    $sql = "delete from ".$tb_fix."_galery where gale_id=$id";
    $q = $anyDB->query($sql);
    // xoa dong thoi cac hinh con cua no
    xoa_hinhcuabosuutap($id);

    if($q){
        
        echo 1;
    }else{
        echo 0;
    }
    exit();
    //
}
/**
 * @author pavennt
 *  xoa hình ảnh kèm them của nộ sưu tập
 * 
 * 
 * 
 */
function  xoa_hinhcuabosuutap($id){
    global $dir_tpl, $tb_fix,$admin_id,$pid,$_c,$temp,$anyDB;
    $sql = "SELECT * FROM ".$tb_fix."_galery_images where gale_id =".$id;
   // echo $sql;
    $q = $anyDB->query($sql);
    $datta = $anyDB->getAll($q);
    //echo "<pre>";    print_r($datta); 
    foreach ($datta as $hinh){
        getlinkhinh($hinh['imag_id']);
    }
}


##################################  images

/**
 * @author pavennt
 * get all images
 * 
 */
function  index_images(){
     global $dir_tpl,$tb_fix,$temp,$anyDB,$site_path;
     $temp->assign("breakcum",breakcum("Quản lý hình ảnh"));
     // category galery 
     $sql = "select * from ".$tb_fix."_galery ";    
     $q = $anyDB->query($sql);
     $datta = $anyDB->getAll($q);
     $temp->assign('gallery',$datta);
         // phân trang
        $pagegroup_size = 10;
        $page=_POST('p');
        if (!$page||$page==0){$page=1;}
        $limit=_POST('limit');
        if($limit==''||$limit>30) {$limit=10;}
        $offset=($page-1)*$limit;
        $li="limit $offset,$limit";
        // tim kiem 
       
        $key_word = isset($_POST['gallery_id']) ? $_POST['gallery_id']:'';
        if (empty($key_word)){
            $where = "";
            
        }else{
            $temp->assign('tukhoa',$key_word);
            $where = " where g.gale_id = $key_word";
        }
        
        $sql = "select COUNT(g.gale_name) as tongso from ".$tb_fix."_galery_images as i
                                           LEFT JOIN ".$tb_fix."_galery as g on i.gale_id = g.gale_id  $where";        
        $q = $anyDB->query($sql);
         $arr = $anyDB->getNext($q);
         $totalRecord = $arr['tongso']; // tong so ban ghi
        
        
	// end
	$sql = "select i.*,g.gale_id,g.gale_name from ".$tb_fix."_galery_images as i
                                           LEFT JOIN ".$tb_fix."_galery as g on i.gale_id = g.gale_id 
                                           $where 
                ORDER BY imag_id DESC $li" ;		
	$q = $anyDB->query($sql);
        $arr = $anyDB->getAll($q);

        $temp->assign('images',$arr);
	$pages = pagenavigator($page,$totalRecord,$limit,$pagegroup_size,"?obj=gallery&act=images&key_word=$key_word");		
        $temp->assign('pages',$pages);
        $temp->assign('base_url',$site_path);
        $_html = $temp->fetch( '_adm_gallery_images.mct' );
     
    return $_html ;
}

/**
 * @author : Nguyễn tiến Pa Ven 
 * add images
 * 
 */
function images_add(){
     global $dir_tpl,$tb_fix,$temp,$anyDB,$site_path;
    
     // get form them hinhs
     if(empty ($_POST['add'])){
     $temp->assign("breakcum",breakcum("Thêm mới hình ảnh "));
     // get bo suu tập
     $sql = "select * from ".$tb_fix."_galery ";
     $q = $anyDB->query($sql);
     $datta = $anyDB->getAll($q);
     $temp->assign('gallery',$datta);
     $temp->assign('thumuc',$dir_tpl);
     
     $html_body = $temp->fetch( '_adm_gallery_images_add.mct' );
     
     
     // add hình 
    }  else {
        
         $galery_id = isset ($_POST['gallery_id'])? $_POST['gallery_id']:'';
         $ten_images = isset ($_POST['ten_images']) ? $_POST['ten_images']:'';
         $mota = isset ($_POST['cat_description']) ? $_POST['cat_description']: '';
         $pro_img = isset($_FILES['cat_img']['name'] ) ? $_FILES['cat_img']['name'] : '';
         $pro_tmp_img = isset($_FILES['cat_img']['tmp_name'] ) ? $_FILES['cat_img']['tmp_name'] : '';
         $url_hinh = uploadIMG($pro_img,$pro_tmp_img,"galery/images");
         if (empty($pro_img) && empty ($galery_id) && empty ($ten_images)&& empty ($url_hinh)){
             
              $html_body =  _html_trans("Có lỗi xảy ra vui nhập đầy đủ thông tin !","?obj=gallery&act=images_add");

         }else{
             $sql = "insert into ".$tb_fix."_galery_images (gale_id,imag_name,imag_url,imag_description)values ('$galery_id','$ten_images','$url_hinh','$mota')";
             $q = $anyDB->query($sql);
             $html_body =  _html_trans("Thêm hình thành công bạn sẽ trở lại form thêm hình!","?obj=gallery&act=images_add");
         }
    }
    
    
    return $html_body ;
}

/**
 * 
 * @author : Pa ven 
 * Xóa nhiều hình ảnh
 */
function xoa_images(){
    $arrid = isset($_GET['id']) ? $_GET['id'] : '';
    if ($arrid != '' ){
        
       $id = explode(",",$arrid);
       for ($i = 0 ; $i < count($id);$i++){
           $linkhinh = getlinkhinh($id[$i]);
       }
       if ($linkhinh){
           
           echo 1;
       }else{
           echo 0;
       }
       exit();
        
    }
    
    
}

/**
 * 
 * pavennt 
 * get link hinh 
 * 
 * 
 * 
 */
function getlinkhinh($id){
    global $anyDB,$tb_fix;
    $sql = "select imag_url from ".$tb_fix."_galery_images where imag_id =$id";
    $q = $anyDB->query($sql);
    $arr = $anyDB->getNext($q);
    $xoa = @unlink($arr['imag_url']);
    $sql = "delete from ".$tb_fix."_galery_images where imag_id=$id";
    $q = $anyDB->query($sql);
    
    return  $q;
    
}
/**
 * @author : pavennt
 * get edit hinh anh 
 * 
 */
function getEdit_images(){
     global $dir_tpl,$tb_fix,$temp,$anyDB,$site_path;
     $temp->assign("breakcum",breakcum("Sửa hình ảnh"));
     $temp->assign('thumuc',$dir_tpl);
     $temp->assign('base_url',$site_path);
     if (empty ($_POST['update_images'])){
         $id = $_GET['id'];
         // category galery 
         $sql = "select * from ".$tb_fix."_galery ";    
         $q = $anyDB->query($sql);
         $datta = $anyDB->getAll($q);
         $temp->assign('gallery',$datta);
         //get data images
         $sql = "select * from ".$tb_fix."_galery_images where imag_id=$id";
         $q = $anyDB->query($sql);
         $arr = $anyDB->getNext($q);
         $temp->assign('images',$arr);
          
         $html_body = $temp->fetch('_adm_gallery_images_edit.mct');
      
       return $html_body;
      
     }else{
         // update du lieu
        $id = $_POST['id'];
        $name_images = isset ($_POST['ten_images']) ? $_POST['ten_images'] : '';
        $galery_id = $_POST['gallery_id'];
        $mota = $_POST['cat_description'];
        $pro_img = isset($_FILES['cat_img']['name'] ) ? $_FILES['cat_img']['name'] : '';
        $pro_tmp_img = isset($_FILES['cat_img']['tmp_name'] ) ? $_FILES['cat_img']['tmp_name'] : '';
        if (empty ($name_images)){
            $html_body =  _html_trans("Bạn chưa điền đầy đủ thông tin!","?obj=gallery&act=images");
        }  
        else if(!empty ($pro_img) && !empty ($galery_id)) {

            $sql = "select imag_url from ".$tb_fix."_galery_images where imag_id =$id";
            $q = $anyDB->query($sql);
            $arr = $anyDB->getNext($q);
            @unlink($arr['imag_url']);    
            $pro_img = uploadIMG($pro_img,$pro_tmp_img,"galery/images");
            // update sql 
            $sql = "update ".$tb_fix."_galery_images set imag_name= '$name_images',imag_url='$pro_img',	imag_description ='$mota', gale_id ='$galery_id' where imag_id=$id" ;
            $ud = $anyDB->query($sql);
            
            $html_body =  _html_trans("Cập nhật thành công !","?obj=gallery&act=images");
            return $html_body;
        }else if(!empty ($name_images) && !empty ($galery_id)){
            $sql = "update ".$tb_fix."_galery_images set imag_name= '$name_images',imag_description ='$mota',gale_id  ='$galery_id' where imag_id=$id" ;
            $ud = $anyDB->query($sql);
            
            $html_body =  _html_trans("Cập nhật thành công !","?obj=gallery&act=images");
            return $html_body;
        }
     }
}